// Tiler Rev.A, by Dave Bushnell
// ZBrush ZScript v1.23b 

[varDef,DocWidth]
[varDef,DocHeight]
[varDef,DocMidX]
[varDef,DocMidY]
[varDef,NumPossTextures,2]
[varDef,Textures(NumPossTextures)]
[varDef,SettingsData(11)]
[varDef,AlternateTextures,0]
[varDef,FlipTexturesH,0]
[varDef,FlipTexturesV,0]
[varDef,ShowMessage,1]
[varDef,WorkLayer]
[varDef,VerifyResult,0]
[varDef,SectionText,"                    Modifiers                    "]
[fontSetSizeSmall][varSet,ButtonWidth,[textCalcWidth,[var,SectionText]]+3][FontSetSizeMedium]
[varSet,sliderWidth,ButtonWidth/2]

[PenMove,0,2][Image,TiTitle.psd][PenMove,15,2][ibutton,"Return to Menu","Load the Script Selection Interface",[fileNameSetNext,"DefaultZScript.txt"][iPress,7427]]
[pd][penMove,0,9]
[penMoveCenter][penMove,-SliderWidth][iSlider,"Horizontal Count",2,1,1,64,"Number of horizontal repetitions (columns)",,0,sliderWidth]
[iSlider,"Vertical Count",2,1,1,64,"Number of vertical repetitions (rows)",,0,sliderWidth]
[pd]
[penMoveCenter][penMove,-ButtonWidth/2][iButton," Draw Textures ","Draw repetitions of the current texture",
	[if,[iGet,texture:itemInfo]=0,
		[if,[MessageYesNo,"There is no texture selected. Continue anyway?"],,[exit]]
	] // end if
	[routineCall,storeSettings]
	[routineCall,drawTheTextures]
	[routineCall,VerifyUndo]
	[routineCall,RecallSettings]
	[if,VerifyResult,[iSet,texture:itemInfo,999]]
,,ButtonWidth] // end iButton
[pd][penMoveCenter][penMove,-ButtonWidth/2][iButton," Draw Alphas ","Draw repetitions of the current alpha",
	[iUnPress,zscript:checkerboard]
	[iPress,alpha:maketexture]
	[routineCall,storeSettings]
	[routineCall,drawTheTextures]
	[routineCall,VerifyUndo]
	
	[iSet,texture:itemInfo,999]
	[if,(VerifyResult = 1),
		[iPress,texture:makeAlpha]
		[iPress,texture:inventory:remove]
		[iSet,texture:itemInfo,999]
	] // end if
	[iPress,texture:inventory:remove]
	[routineCall,RecallSettings]
	[if,VerifyResult,[iSet,alpha:itemInfo,999]]
,,ButtonWidth] // end iButton
[pd][penMove,0,6][penMoveCenter][penMove,-ButtonWidth/2][iButton," Show and Tell ","Brief Tutorial",
	[routineCall,storeSettings]
	[iUnPress,zscript:flipcolumns][iUnPress,zscript:fliprows][iUnPress,zscript:checkerboard]
	[Note,"This ZScript shows you the selected texture as it would look tiled a number of times...",28601,12]
	[Note,"...and then adds the tiled version to your Texture palette if you desire.",texture:,11]
	[Note,"Begin by selecting a texture.",,10]
	[iSet,texture:itemInfo,23]
	[Note,"Set the \Cffa000Horizontal Count\Cc0c0c0 slider to the number of columns you'd like to see drawn...",zscript:horizontalcount,10]
	[iSet,zscript:horizontalcount,5]
	[Note,"...and the \Cffa000Vertical Count\Cc0c0c0 slider to the number of rows.",zscript:verticalcount,10]
	[iSet,zscript:verticalcount,4]
	[Note,"Press the \Cffa000Draw Textures\Cc0c0c0 button to see the current texture tiled using these numbers.",zscript:drawtextures,12]
	[routineCall,drawTheTextures]
	[Note,"The texture is now tiled and can be grabbed (added to the Texture palette) when prompted, or discarded.",1004,20]
	[iKeyPress,'N',[routineCall,VerifyUndo]]
	[routineCall,RecallSettings]
	[Note,"You can do the same for your selected alpha, by pressing the \Cffa000Draw Alphas\Cc0c0c0 button.",28701,-1][Note,,zscript:drawalphas,15]
,,ButtonWidth] // end iButton Show and Tell
[pd][penMove,0,6]
[penMoveCenter][PenMove,-ButtonWidth/2][FontSetSizeSmall][sectionBegin,[var,SectionText]][pd][penMove,0,6]
[FontSetSizeMedium]
[penMoveCenter][penMove,-SliderWidth][iSwitch," Flip Columns ",0,"Flip alternating tiles horizontally",
	[varSet,FlipTexturesH,1]
	[iUnPress,1]
	,
	[varSet,FlipTexturesH,0]
,,SliderWidth-10] // end iSwitch
[penMove,20][iSwitch," Checkerboard ",0,"Alternate textures, checkerboard-style",
	[varSet,AlternateTextures,1]
	[iUnPress,-1][iUnPress,1]
	[iEnable,2][iEnable,3][iEnable,4][iEnable,5]
	[iSetMax,2,[iGetMax,texture:itemInfo]]
	[iSetMax,4,[iGetMax,texture:itemInfo]]
	,
	[varSet,AlternateTextures,0]
	[iDisable,2][iDisable,3][iDisable,4][iDisable,5]
,,SliderWidth-10] // end iSwitch
[pd][penMoveCenter][penMove,-SliderWidth][iSwitch," Flip Rows ",0,"Flip alternating tiles vertically",
	[varSet,FlipTexturesV,1]
	[iUnPress,-1]
	,
	[varSet,FlipTexturesV,0]
,,SliderWidth-10] // end iSwitch
[penMove,20][iSlider," First Texture ",[iGet,texture:itemInfo],1,0,[iGetMax,texture:ItemInfo],"Item-Info Number of First Texture",
	[delay,.11]
,1,SliderWidth-10] // end iSlider
[iButton,"P","Use Current Texture as the First Texture",
	[if,[iGetMax,-1]<[iGet,texture:itemInfo],[iSetMax,-1,[iGet,texture:itemInfo]]]
	[iSet,-1,[iGet,texture:itemInfo]]
,1]
[pd][penMoveCenter][penMove,9][iSlider," Second Texture ",0,1,0,[iGetMax,texture:ItemInfo],"Item-Info Number of Second Texture",
	[delay,.11]
,1,SliderWidth-10] // end iSlider
[iButton,"P","Use Current Texture as the Second Texture",
	[if,[iGetMax,-1]<[iGet,texture:itemInfo],[iSetMax,-1,[iGet,texture:itemInfo]]]
	[iSet,-1,[iGet,texture:itemInfo]]
,1]
[penMove,0,4][penMoveCenter][penMove,-SliderWidth][iSwitch," Hide Confirm Message ",0,"Hide 'Okay To Grab' Message (if pressed)",
	[varSet,ShowMessage,0]
,
	[varSet,ShowMessage,1]
,0,SliderWidth-10] // end iSwitch

[sectionEnd][FontSetSizeMedium][pd]
\C00e0e0About This ZScript:
\Cc0c0c0This ZScript can be loaded at any time while you're working. It draws the currently-selected texture (or alpha) and repeats it a chosen number of times, in multiple rows and columns. It then grabs the canvas and adds it to the Texture (or Alpha) palette.

Set the \Cffa000Horizontal Count\Cc0c0c0 (number of columns) in the first slider, and the \Cffa000Vertical Count\Cc0c0c0 (number of rows) in the second slider. Then press \Cffa000Draw Textures\Cc0c0c0 to fill the rows and columns with the currently-selected [buttonFind,28601,Texture], and add the result to the Texture palette. Press \Cffa000Draw Alphas\Cc0c0c0 to fill the rows and columns with the currently-selected [buttonFind,28701,Alpha], and add the result to the Alpha palette.

Alpha tiling is useful because you can placed tiled textures on a 3D object using sliders in the Tool:Modifiers sub-palette; this is a way to create matching tiled alphas.

The \Cffa000Show and Tell\Cc0c0c0 button gives you a brief tutorial on how this ZScript is used.

\C00e0e0About the Modifiers:
\Cc0c0c0You can choose to flip each column horizontally and/or flip each row vertically. This creates a unique effect, and can also make a seamless texture (left edge matches the right, top edge matches the bottom) from a non-seamless tile. To make sure the texture is seamless, use even numbers for the Horizontal and Vertical Counts.

You can also create a "checkerboard" effect, which draws two textures in alternation, by pressing the \Cffa000Checkerboard\Cc0c0c0 switch. When this button is pressed, you can specify the two textures by entering their [buttonFind,Texture:itemInfo,"Item Info"] numbers in the next two sliders. If you prefer, you can choose each texture in the Texture palette, and press the small \Cffa000"P"\Cc0c0c0 button next to each slider to pick that texture.

You cannot activate Flipping and Checkerboard drawing at the same time.

Press the \Cffa000Hide Confirm Message\Cc0c0c0 switch to inhibit the confirmation message that normally appears after drawing.

[fontSetSizeSmall][FontSetOpacity,.5]Tiler Revision A, created by Dave Bushnell


// ========================= ROUTINES FOLLOW

[routineDef,drawTheTextures,
	[varSet,Textures(0),zscript:firsttexture]
	[varSet,Textures(1),zscript:secondtexture]
	[iPress,tool:simpleBrush]
	[iSet,alpha:itemInfo,0]
	[iSet,stroke:itemInfo,1]
	[iPress,draw:zadd][iPress,draw:mrgb]
	[iSet,draw:RGBIntensity,100]
	[iSet,draw:ZIntensity,100]
	[iSet,material:itemInfo,0]
	[routineCall,makeNewLayer]
	
	[varSet,docWidth,document:modifiers:width]
	[varSet,docHeight,document:modifiers:height]
	[varSet,docMidX,docWidth/2]
	[varSet,docMidY,docHeight/2]
	
	[varSet,lNumHReps,[iGet,zscript:horizontalcount]]
	[varSet,lNumVReps,[iGet,zscript:verticalcount]]
	[varSet,lCellWidth,int(docWidth/lNumHReps/2)]
	[varSet,lCellHeight,int(docHeight/lNumVReps/2)]
	
	[varSet,lHpos,int(docMidX-(lCellWidth*(lNumHReps-1)))]
	[varSet,lVpos,int(docMidY-(lCellHeight*(lNumVReps-1)))]
	
	[if,AlternateTextures,[iSet,texture:itemInfo,Textures(0)]]
	[varSet,lSignH,1]
	[varSet,lSignV,1]
	[canvasClick,lHpos,lVpos,lHpos,lVpos+(lCellHeight*1.414)]
	[transformSet,lHpos,lVpos,-4000,lCellWidth,lCellHeight,100]
		[varSet,liy,0]
		[varSet,lRowiTxt,0]
		[loop,lNumVReps,
			[varSet,lix,0]
			[varSet,lColiTxt,lRowiTxt]
			[if,lSignH<0,[iPress,texture:flph]]
			[loop,lNumHReps,
				[varSet,liHpos,lHpos+(lix*lCellWidth*2)]
				[varSet,liVpos,lVpos+(liy*lCellHeight*2)]
				[iPress,transform:snapshotObject]
				[transformSet,liHpos,liVpos,-4000,lCellWidth,lCellHeight,100]
				[varInc,lix]
				[if,AlternateTextures,
					[varSet,lColiTxt,(lColiTxt<1)]
					[iSet,texture:itemInfo,Textures(lColiTxt)]
				] // end if
				[if,FlipTexturesH,[varSet,lSignH,-lSignH][iPress,texture:flph]]
			] // end loop
			[varInc,liy]
			[if,AlternateTextures,
				[varSet,lRowiTxt,(lRowiTxt<1)]
				[iSet,texture:itemInfo,Textures(lRowiTxt)]
			] // end if
			[if,FlipTexturesV,[varSet,lSignV,-lSignV][iPress,texture:flpv]]
		] // end loop
		[if,lSignH<0,[iPress,texture:flph]]
		[if,lSignV<0,[iPress,texture:flpv]]
] // end DrawTheTextures

[routineDef,StoreSettings,
	[varSet,SettingsData(0),tool:itemInfo]
	[varSet,SettingsData(10),texture:itemInfo]
	[iPress,tool:simpleBrush]
	[varSet,SettingsData(1),stroke:itemInfo]
	[varSet,SettingsData(2),alpha:itemInfo]
	[varSet,SettingsData(3),0]
	[varSet,lix,4356]
	[loop,3,
		[if,[iGet,lix],[varSet,SettingsData(3),lix]]
		[varInc,lix]
	]
	[varSet,SettingsData(4),0]
	[varSet,lix,4359]
	[loop,3,
		[if,([iGet,lix]),[varSet,SettingsData(4),lix]]
		[varInc,lix]
	]
	[varSet,SettingsData(5),draw:rgbintensity]
	[varSet,SettingsData(6),color:r]
	[varSet,SettingsData(7),color:g]
	[varSet,SettingsData(8),color:b]
	[varSet,SettingsData(9),material:itemInfo]
	[iSet,texture:itemInfo,SettingsData(10)]
] // end routine StoreSettings

[routineDef,RecallSettings,
	[iPress,tool:simpleBrush]
	[iSet,stroke:itemInfo,SettingsData(1)]
	[iSet,alpha:itemInfo,SettingsData(2)]
	[if,(SettingsData(3)>0),[iPress,SettingsData(3)],[iPress,4356][iUnPress,4356]]
	[if,(SettingsData(4)>0),[iPress,SettingsData(4)],[iPress,4359][iUnPress,4359]]
	[iSet,draw:rgbIntensity,SettingsData(5)]
	[iColorSet,SettingsData(6),SettingsData(7),SettingsData(8)]
	[iSet,material:ItemInfo,SettingsData(9)]
	[iSet,tool:itemInfo,SettingsData(0)]
] // end RecallSettings

[routineDef,makeNewLayer,
	[varSet,lix,3842]
	[loop,16,
		[if,[isDisabled,lix],[varSet,WorkLayer,lix][loopExit]]
		[varInc,lix]
	]
	[iPress,layer:inventory:create]
	[iKeyPress,SHIFT,[iClick,WorkLayer,3,3]]
] // end makeNewLayer
[routineDef,VerifyUndo,
	[if,[var,ShowMessage],
		[if,[MessageYesNo,"Okay to grab this image? (Press YES to grab, NO to remove)","Grab or Remove? (This cannot be undone)"],
			[RoutineCall,GrabScreen]	
		] // end if
	, // else
		[RoutineCall,GrabScreen]
	] // end if
	[iKeyPress,SHIFT,[iPress,WorkLayer]]
	[iKeyPress,13,[iPress,layer:inventory:delete]]		
] // end VerifyUndo

[RoutineDef,GrabScreen,
	[iPress,tool:mrgbzgrabber]
	[iUnPress,draw:zadd][iPress,draw:mrgb]
	[canvasClick,docMidX,docMidY,docWidth,docHeight]
	[varSet,VerifyResult,1]	
] // end GrabScreen